package com.yc.javaee.servlet.d0614;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Locale;

@WebServlet(name = "NameFilterServlet", value = "/nameFilter.s")
public class NameFilterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        String json = "{ \"code\":%d, \"msg\":\"%s\", \"data\":[%s]}";
        String returnNames = "";
        for (String s : CheckNameServlet.names) {
            // 忽略大小写比较开头字符串
            if(s.toLowerCase().startsWith(name.toLowerCase())){
                if(returnNames.length()>0){
                    returnNames += ",";
                }
                returnNames += "\"" + s + "\"";
            }
        }
        json = String.format(json, 1, "查询姓名成功!", returnNames);
        out.append(json);
        /**
         *  返回的json对象结构 说明
         *   {
         *      code : 1,
         *      msg : "xxxx",
         *      data : [ "xxx", "xyy", "xzz", ... ]
         *   }
         *
         *
         */
    }
}
